<script lang="ts" setup>
import {reactive} from "vue";
import {type LoginDTO, type ResponseInterface} from "@/type/models";
import {getAPI, mockServiceURL} from "@/api/MyAxiosAPI";
import router from "@/router";
import {ElMessage} from "element-plus";
// api
const MOCK = false
const API = getAPI(MOCK)
//--- 登录
let loginForm = reactive<LoginDTO>({
  email: "",
  password: "",
})

function onSubmit() {
  // 检查输入框是否符合规范
  if (loginForm.email.length == 0 || loginForm.password.length == 0) {
    ElMessage.error("邮箱或密码不能为空！")
    return
  }
  API({
    url: mockServiceURL(MOCK, "user", "/user/login"),
    method: "post",
    data: loginForm
  }).then((res: any) => {
    let r: ResponseInterface = res.data;
    if (r.code == 200) {
      localStorage.setItem("token", r.data)
      router.push("/home")
    }
  })
}

//--- end 登录
</script>

<template>
  <el-row class="center-text" justify="center">
    <el-col :offset="8" :span="8">
      <h1>登录</h1>
    </el-col>
    <el-col :span="8">
    </el-col>
  </el-row>
  <el-row>
    <el-col :offset="9" :span="6">
      <el-form :model="loginForm">
        <el-form-item label="邮箱">
          <el-input v-model="loginForm.email"/>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="loginForm.password" type="password"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">Create</el-button>
          <router-link to="/register">
            <el-button link>没有账号，去注册</el-button>
          </router-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style scoped>
.center-text {
  text-align: center;
}
</style>